package com.tjetc.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @since 2025-10-20
 */
@Component
public class LockUtil {

    @Autowired
    private static RedisTemplate<String, Object> redisTemplate;

    // 获取锁
    public static boolean tryLock(String key){
        Boolean isSuccess = redisTemplate.opsForValue().setIfAbsent(key, "1", Duration.ofSeconds(5));
        return Boolean.TRUE.equals(isSuccess);
    }


    // 释放锁
    public static void unLock(String key) {
        redisTemplate.delete(key);
    }

}
